package com.hongning.railway.ui.main.fragments.college;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqNewsBean;
import com.hongning.railway.entity.resbean.BaseResExam;
import com.hongning.railway.entity.resbean.ResExamCompleteBean;
import com.hongning.railway.entity.resbean.ResExamItem;
import com.hongning.railway.ui.main.fragments.college.itemviewModel.ExamCopleteItemViewModel;
import com.hongning.railway.ui.uichangeobserable.ListUIChangeObserable;
import com.hongning.railway.util.Apputils;

import java.util.List;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class ExamCompleteFragmentViewModel extends BaseViewModel<AppRepository> {
    private int page=1;
    public ListUIChangeObserable uc = new ListUIChangeObserable();
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            itemBinding.set(BR.viewModel, R.layout.item_exam_vip_completed);
        }
    });

    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            page =1;
            loadExamCompleteData();
        }
    });

    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            loadExamCompleteData();
        }
    });
    public ObservableList<MultiItemViewModel> viewlist = new ObservableArrayList<>();

    public ExamCompleteFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }


    public void loadExamCompleteData(){
        ReqNewsBean reqNewsBean = new ReqNewsBean();
        reqNewsBean.setPageNum(page);
        reqNewsBean.setPageSize(Constant.PAGE_SIZE);
        reqNewsBean.setStaffId(Apputils.getStaffid());
        model.getExamCompleteList(reqNewsBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<List<ResExamCompleteBean>>() {
                    @Override
                    public void onResult(List<ResExamCompleteBean> resExamCompleteBeans) {
                        if (page==1){
                            uc.finishRefresh.call();
                            viewlist.clear();
                            if (resExamCompleteBeans.isEmpty()){
                                uc.contentEmpty.call();
                            }else {
                                uc.contentNotEmpty.call();
                            }
                        }else {
                            uc.finishLoadMore.call();
                            if (resExamCompleteBeans.size()<Constant.PAGE_SIZE){
                                uc.finishLoadMoreEnd.call();
                            }
                        }
                        page++;
                        for (ResExamCompleteBean data :resExamCompleteBeans) {
                            viewlist.add(new ExamCopleteItemViewModel(ExamCompleteFragmentViewModel.this,data));
                        }
                    }
                });
    }

}
